/* spectrometer.c 
gcc -o extprg -I/usr/local/lib/erlang/lib/erl_interface-3.5.9/include/ -L/usr/local/lib/erlang/lib/erl_interface-3.5.9/lib/ spectrometer.c erl_comm.c spectrometer_ei.c -lerl_interface -lei -lpthread -framework GDAL -D MAC_OS_X
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#ifdef MAC_OS_X
    #include <GDAL/gdal.h>
    #include <GDAL/cpl_string.h>
#else
    #include "gdal.h"
    #include "cpl_string.h"
#endif

#include "ai_image.h"

void free_band_data(band *pBand) {
    CPLFree(pBand->data);
    pBand->data = NULL;
}

int spectrometer(char* file_name,byte* data, int size, band *pBand1, band *pBand2, band *pBand3) {
    char mem_name[256] = "/vsimem/";    //"/vsimem/work.dat"
    strcat(mem_name, file_name);
    VSIFCloseL( VSIFileFromMemBuffer(mem_name , data, size, FALSE));
    GDALDatasetH hDS = GDALOpen( mem_name, GA_ReadOnly ); 

    GDALRasterBandH hBand1, hBand2, hBand3;
    //int nXSize, nYSize;
    hBand1 = GDALGetRasterBand(hDS, 1);	//GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(hBand)) = 'red'
    hBand2 = GDALGetRasterBand(hDS, 2); //GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(hBand)) = 'green'
    hBand3 = GDALGetRasterBand(hDS, 3); //GDALGetColorInterpretationName(GDALGetRasterColorInterpretation(hBand)) = 'blue'
    //GDALGetBlockSize(hBand1, &nXSize, &nYSize);
    pBand1->xSize = GDALGetRasterBandXSize(hBand1);
    pBand1->ySize = GDALGetRasterBandYSize(hBand1);
    pBand1->data = (byte *)CPLMalloc(sizeof(byte) * pBand1->xSize * pBand1->ySize);
 
    pBand2->xSize = GDALGetRasterBandXSize(hBand2);
    pBand2->ySize = GDALGetRasterBandYSize(hBand2);
    pBand2->data = (byte *)CPLMalloc(sizeof(byte) * pBand2->xSize * pBand2->ySize);
    
    pBand3->xSize = GDALGetRasterBandXSize(hBand3);
    pBand3->ySize = GDALGetRasterBandYSize(hBand3);
    pBand3->data = (byte *)CPLMalloc(sizeof(byte) * pBand3->xSize * pBand3->ySize);
    
    int ss = sizeof(byte) * pBand2->xSize * pBand2->ySize;

    GDALRasterIO(hBand1, GF_Read, 0, 0, pBand1->xSize, pBand1->ySize, pBand1->data, pBand1->xSize, pBand1->ySize, GDT_Byte, 0, 0);
    GDALRasterIO(hBand2, GF_Read, 0, 0, pBand2->xSize, pBand2->ySize, pBand2->data, pBand2->xSize, pBand2->ySize, GDT_Byte, 0, 0);
    GDALRasterIO(hBand3, GF_Read, 0, 0, pBand3->xSize, pBand3->ySize, pBand3->data, pBand3->xSize, pBand3->ySize, GDT_Byte, 0, 0);

    GDALClose(hDS);
    VSIUnlink( mem_name );
    
    return ss;
}

/*
* http://www.gdal.org/frmt_mem.html
*/
GDALDatasetH createMemoryDataset(char *file_name, int nBands, int nXSize, int nYSize) {
    GDALDriverH memDriver = GDALGetDriverByName("MEM");

    char pszDstFilename[256] = "mem_";
    strcat(pszDstFilename, file_name);
    GDALDatasetH memDS = GDALCreate(memDriver, pszDstFilename, 
                nXSize, nYSize, nBands, GDT_Byte, 
                NULL);  // There are no supported creation options.
    return memDS;
}
    
void copyToMemory(GDALDatasetH memDS, GByte *bandData, int nthBands, int nXSize, int nYSize) {
    GDALRasterBandH dstBand = GDALGetRasterBand(memDS, nthBands);
    GDALRasterIO(dstBand, GF_Write, 
            0, 0, nXSize, nYSize, bandData, 
            nXSize, nYSize, GDT_Byte, 
            0, 0);
}
    
void saveAsPngFile(GDALDatasetH hDstDS, char *file_name) {
    GDALDriverH tmpDriver = GDALGetDriverByName("PNG");
    GDALDatasetH tmpDS = GDALCreateCopy(tmpDriver, file_name, hDstDS, FALSE,
                            NULL, NULL, NULL);
    GDALClose(tmpDS);        
}     

int foo(int x) {
    return x+1;
}

